package com.situ.staff.util;

import java.io.IOException;
import java.io.Writer;
import java.lang.reflect.Field;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;

import org.json.JSONArray;
import org.json.JSONObject;

//import com.alibaba.fastjson.parser.Feature;
//import com.mysql.cj.xdevapi.JsonArray;

public class FmtRequest {

	/**
	 * 根据请求的参数情况，反射到实体类对象中
	 * 
	 * @param <T>     需要反射生成的实体类对象
	 * @param request 请求对象
	 * @param clazz   需要反射的实体类的类的描述
	 * @return
	 */
	public static <T> T parseModel(HttpServletRequest request, Class<T> clazz) {
		T obj = null;
		try {
			obj = clazz.newInstance();
		} catch (InstantiationException | IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		Map<String, String[]> map = request.getParameterMap();
//		System.out.println(map);
//		for(Map.Entry<String, String[]> entry :map.entrySet()) {
//			System.out.println(entry.getKey()+" "+entry.getValue()[0]);
//		}
		for (Entry<String, String[]> entry : map.entrySet()) {
			String name = entry.getKey();
			if ("action".equals(name))
				continue;
			Field field;
			try {
				field = clazz.getDeclaredField(name);
				field.setAccessible(true);
				field.set(obj, entry.getValue()[0]);
			} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
				e.printStackTrace();
			}
		}
		return obj;

	}

	/**
	 * 根据映射关系生成实体类对象
	 * 
	 * @param <T>
	 * @param request
	 * @param clazz
	 * @param fields  key=属性名，value=参数名
	 * @return
	 */
	public static <T> T parseModel(HttpServletRequest request, Class<T> clazz, Map<String, String> fields) {
		T obj = null;
		try {
			obj = clazz.newInstance();
		} catch (InstantiationException | IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		for (Entry<String, String> entry : fields.entrySet()) {
			String name = entry.getKey();
			String val = request.getParameter(entry.getValue());//从请求中获取值
			try {
				Field field = clazz.getDeclaredField(name);//获取实体类中的字段
				field.setAccessible(true);
				field.set(obj, val);//给对象的field字段赋值
			} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return obj;
	}

	public static void write(Writer wr, String val) {
		try {
			wr.write(val);
			wr.flush();
			wr.close();
			wr = null;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void write(Writer wr, Object val) {
		if (val instanceof Collection<?>) {
			write(wr, new JSONArray((Collection<?>) val).toString());
		} else if (val instanceof String) {
			write(wr, val.toString());
		} else if (val instanceof Map<?, ?>) {
			write(wr, new JSONObject((Map<?, ?>) val).toString());

		} else {
			write(wr, new JSONObject(val).toString());
		}

	}

}
